package com.pozu.new_retail.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pozu.new_retail.dao.mapper.AppSettingMapper;
import com.pozu.new_retail.dao.mapper.CodeSettingMapper;
import com.pozu.new_retail.dao.mapper.CommunitySettingMapper;
import com.pozu.new_retail.dao.mapper.DiscountSettingMapper;
import com.pozu.new_retail.dao.mapper.GoodsMapper;
import com.pozu.new_retail.dao.mapper.GoodsSaleTypeSettingMapper;
import com.pozu.new_retail.dao.mapper.NavigationBarMapper;
import com.pozu.new_retail.dao.model.AppSetting;
import com.pozu.new_retail.dao.model.GoodsSaleTypeSetting;
import com.pozu.new_retail.dao.vo.AdvertisementVo;
import com.pozu.new_retail.dao.vo.AppSettingVo;
import com.pozu.new_retail.service.AppSettingService;

@Service("appSettingService")
public class AppSettingServiceImpl implements AppSettingService{

	@Autowired
	private AppSettingMapper appSettingMapper;
	@Autowired
	private NavigationBarMapper navigationBarMapper;
	@Autowired
	private DiscountSettingMapper discountSettingMapper;
	@Autowired
	private CommunitySettingMapper communitySettingMapper;
	@Autowired
	private CodeSettingMapper codeSettingMapper;
	@Autowired
	private GoodsSaleTypeSettingMapper goodsSaleTypeMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Override
	public AppSetting queryByAppId(int appId) {
		return appSettingMapper.queryByAppId(appId);
	}

	@Override
	public AppSettingVo querySettingByAppId(int appId) {
		
		AppSetting appSetting = queryByAppId(appId);
		
		AppSettingVo vo = new AppSettingVo();
		
		if (null != appSetting) {
			
			vo.setAppId(appId);
			vo.setCategoryFlag(appSetting.getCategoryFlag());
			vo.setChatFlag(appSetting.getChatFlag());
			vo.setColor(appSetting.getColor());
			vo.setDisplay(appSetting.getDisplay());
			vo.setImg(appSetting.getImg());
			vo.setNavigationBar(navigationBarMapper.queryByAppId(appId));
			vo.setDiscountSetting(discountSettingMapper.queryByAppId(appId));
			vo.setCommunitySetting(communitySettingMapper.queryByAppId(appId));
			vo.setExpressMoney(appSetting.getExpressMoney());
			
			vo.setCodeSettings(codeSettingMapper.queryAllByAppId(appId));
			
			
			GoodsSaleTypeSetting goodsSaleTypeSetting = goodsSaleTypeMapper.queryByAppId(appId);
			
			if (null != goodsSaleTypeSetting) {
				
				HashMap<String, Object> map = new HashMap<String, Object>();

				map.put("appId", appId);
				map.put("saleType", 0);
				
				if (goodsMapper.queryCountByMap(map) == 0) {
					goodsSaleTypeSetting.setName1(null);
				}
				
				map.put("saleType", 1);
				if (goodsMapper.queryCountByMap(map) == 0) {
					goodsSaleTypeSetting.setName2(null);
				}
				
				map.put("saleType", 2);
				if (goodsMapper.queryCountByMap(map) == 0) {
					goodsSaleTypeSetting.setName3(null);
				}
				vo.setGoodsSaleTypeSetting(goodsSaleTypeSetting);

			}
			
		}
		
		return vo;
	}

	@Override
	public List<AdvertisementVo> queryAds(int appId) {
		
		List<AdvertisementVo> ads = new ArrayList<AdvertisementVo>();
		
		String roleIds = appSettingMapper.queryRoleIdsByAppId(appId);
		
		if(null == roleIds || !roleIds.contains(",")) {
			return null;
		}
		
		String[] roleId = roleIds.split(",");
		
		ads.addAll(appSettingMapper.queryByRoleId(Integer.parseInt(roleId[0])));
		ads.addAll(appSettingMapper.queryByRoleId(Integer.parseInt(roleId[1])));
		ads.addAll(appSettingMapper.queryByRoleId(Integer.parseInt(roleId[2])));
		
		return ads;
	}

}
